package com.paobuji.aidebao.myapplication.video;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;

import com.paobuji.aidebao.myapplication.R;

/**
 * Created by Lijianbao on 2018/12/26.
 */

public class Video implements SurfaceHolder.Callback
        , MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener,
        MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnSeekCompleteListener {
    String TAG = "VedioActivity >>>  Video";
    Activity mContext;
    MediaPlayer mediaPlayer;
    SurfaceView surface_view;

    TextView waitTxt;
    int width1;
    private int currentPosition = 0;
    //点击纵坐标
    private float dY = 0;
    //点击横坐标
    private float dX = 0;
    //抬起纵坐标
    private float uY = 0;
    //抬起横坐标
    private float uX = 0;
    public Video(Activity context){
        mContext = context;

        initView();
//        setListener();
    }

    protected void initView() {
//        surface_view = (SurfaceView) mContext.findViewById(R.id.surface_view);
//        waitTxt = (TextView) mContext.findViewById(R.id.waitTxt);
//        mediaPlayer = new MediaPlayer();
//        surface_view.getHolder().addCallback(this);
    }
    public int getWidth() {
        width1 = mContext.getWindowManager().getDefaultDisplay().getWidth();
        return width1;
    }
    private void setListener() {

        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setOnErrorListener(this);
        mediaPlayer.setOnBufferingUpdateListener(this);
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.setOnSeekCompleteListener(this);
        surface_view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        dX = event.getX();
                        dY = event.getY();

                        break;
                    case MotionEvent.ACTION_UP:
                        uY = event.getY();
                        uX = event.getX();
                        if (dX > getWidth() / 2) {//声音控制
                            if (Math.abs(uY - dY) > 35)
                                setVolume(uY - dY);
                        } else if (dX <= getWidth() / 2) {//亮度控制
                            if (Math.abs(uY - dY) > 35)
                            setLight(uY - dY);
                        }

                        if(dX - uX < -50){
                           //加速
                            onTouchSport.onSetSpeedNoSlope(1);
                        }else if(dX - uX > 50){
                           //减速
                            onTouchSport.onSetSpeedNoSlope(-1);
                        }else if(dX - uX < 10){
                            onTouchSport.onSufaceClick();
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:

                        break;
                }
                return true;
            }
        });

    }
    //手势调节音量
    private void setVolume(float vol) {
        if (vol < 0) {//增大速度
            Log.e(TAG, "增大速度");
            onTouchSport.onSetSpeed(1);
        } else if (vol > 0) {//降低速度
            Log.e(TAG, "降低速度");
            onTouchSport.onSetSpeed(-1);
        }
    }

    /**
     * 手势设置屏幕亮度
     * 设置当前的屏幕亮度值，及时生效 0.004-1
     * 该方法仅对当前应用屏幕亮度生效
     */
    private void setLight(float vol) {
        if (vol < 0) {//增大坡度
            Log.e(TAG, "降低坡度");
            onTouchSport.onSetSLope(1);
        } else if (vol > 0) {//降低坡度
            Log.e(TAG, "增大坡度");
            onTouchSport.onSetSLope(-1);
        }
    }


    public String suijishu(){
        int c = (int)(1+Math.random()*(6-1+1));
        return "http://djq.djq42195.com/video"+ c + ".mp4";
    }

    /**
     * 开始播放
     *
     * @param msec 播放初始位置
     */
    protected void play(final int msec) {
        // 获取视频文件地址
        /*String path1 = "http://djq.djq42195.com/Woods.mp4";
        String path2 = "http://djq.djq42195.com/Park.mp4";*/
       /* File file = new File(path);
        if (!file.exists()) {
            Toast.makeText(this, "视频文件路径错误", Toast.LENGTH_SHORT).show();
            return;
        }*/
        try {

            if(mediaPlayer == null){
                mediaPlayer = new MediaPlayer();
            }
            //使mediaPlayer重新进入ide状态
            mediaPlayer.reset();

            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            // 设置播放的视频源,本地视频
            //mediaPlayer.setDataSource(file.getAbsolutePath());
            //网络视频
            mediaPlayer.setDataSource(suijishu());
            // 设置显示视频的SurfaceHolder
            mediaPlayer.setDisplay(surface_view.getHolder());
            //同步准备,播放本地
            //mediaPlayer.prepare();
            //循环播放
            mediaPlayer.setLooping(true);
            Log.i(TAG, "开始装载");
            //因为是网络视频 这里用异步准备
            mediaPlayer.prepareAsync();
            waitTxt.setVisibility(View.VISIBLE);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 重新开始播放
     */
    protected void replay() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.seekTo(0);
            Log.i(TAG, "重新播放");
            return;
        }
        play(0);
    }
    /**
     * 停止播放
     */
    public void stop() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
           // mediaPlayer = null;
        }
    }
    public void onPause() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
    }

    public void onStart() {
        if (!mediaPlayer.isPlaying()) {
            mediaPlayer.start();
        }
    }


    @Override
    public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {
        Log.i(TAG,"在网络视频流缓冲变化时调用");
    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        // 在播放完毕被回调
        Log.i(TAG,"在播放完毕被回调");
        // 按照初始位置播放
    }

    @Override
    public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
        // 发生错误重新播放
        Log.i(TAG,"播放发生错误");
        replay();
        return false;
    }

    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        waitTxt.setVisibility(View.GONE);

        Log.i(TAG, "装载完成");
        mediaPlayer.start();
        // 按照初始位置播放
        mediaPlayer.seekTo(0);
    }

    @Override
    public void onSeekComplete(MediaPlayer mediaPlayer) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        Log.i(TAG, "SurfaceHolder 被创建");
        //if (currentPosition > 0) {
        // 创建SurfaceHolder的时候，如果存在上次播放的位置，则按照上次播放位置进行播放
        play(currentPosition);
        currentPosition = 0;
        // }
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        Log.i(TAG, "SurfaceHolder 大小被改变");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        Log.i(TAG, "SurfaceHolder 被销毁");
        // 销毁SurfaceHolder的时候记录当前的播放位置并停止播放
        try{
            if (mediaPlayer != null ){
                if(mediaPlayer.isPlaying()){
                    currentPosition = mediaPlayer.getCurrentPosition();
                    mediaPlayer.stop();
                }
            }
        }catch (Exception e){

        }

    }

    public void onDestroy() {

        try{
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.release();
        }catch (Exception e){

        }


        mediaPlayer.setOnCompletionListener(null);
        mediaPlayer.setOnErrorListener(null);
        mediaPlayer.setOnBufferingUpdateListener(null);
        mediaPlayer.setOnPreparedListener(null);
        mediaPlayer.setOnSeekCompleteListener(null);
        surface_view.setOnTouchListener(null);
    }


    public OnTouchSport onTouchSport;
    public void onTouchSportListener(OnTouchSport onTouchSport){
        this.onTouchSport = onTouchSport;
    }
}
